package net.wanglu.www.zzz.rule.frag;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.regex.Pattern;

import net.wanglu.www.zzz.rule.BadEgg;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.trees.TypedDependency;

public class AfterthoughtFragments extends _FragmentsRule {

	Pattern afterthought = Pattern.compile("^(\\bEspecially\\b|\\bExcept\\b|\\bExcluding\\b|\\bFor example\\b|\\bFor instance\\b|\\bIncluding\\b|\\bLike\\b|\\bSuch as\\b)");
	@Override
	public Object deviseRightSolution(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		sb.clear();
		sb.append("<p>");
		sb.append("修改这种残缺句有两种方法. 一种是添加主语和谓语, 这种方法适用于以for example, for instance开头的残缺句. 例如:");
		sb.append("</p>");
		sb.append("<p>");
		sb.append("误: For example, leaky pens, candy wrappers, dollar bills, and paperclips.");
		sb.append("</p>");
		sb.append("<p>");
		sb.append("正: For example, the desk drawer contained leaky pens, candy wrappers, dollar bills, and paperclips.");
		sb.append("</p>");
		sb.append("<p>");
		sb.append("第二种方法是把残缺句附在独立主句的后面, 这种方式适用于以except, excluding, including, like, such as开头的残缺句. 例如:");
		sb.append("</p>");
		sb.append("<p>");
		sb.append("误: Such as leaving the stove on and teasing mean dogs.");
		sb.append("</p>");
		sb.append("<p>");
		sb.append("正: John has many unsafe habits, such as leaving the stove on and teasing mean dogs.");
		sb.append("</p>");
		return sb.toString();
	}

	@Override
	public Object examine(Object... objects) throws Exception {
		// TODO Auto-generated method stub		
		HashMap<?, ?> preprocessData = (HashMap<?, ?>) objects[0];
		String sentence = (String) preprocessData.get("sentence");
		Tree root = (Tree) preprocessData.get("root");
		Collection<?> tdl = (Collection<?>) preprocessData.get("tdl");
		
		Tree s = root.getChild(0);
		
		if ( "FRAG".equals(s.value()) && afterthought.matcher(sentence).find()) {
			for( Iterator<?> itr = tdl.iterator(); itr.hasNext();) {
				TypedDependency TD = (TypedDependency) itr.next();
				if ( "nsubj".equals(TD.reln().getShortName())) {
					return null;
				}
			}
			BadEgg egg = new BadEgg();
			egg.setViolatedRule(this);
			egg.whereIsWrong = sentence;
			return egg;
		} else {
			return null;
		}
	}

	@Override
	public Object whyItsWrong(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		sb.clear();
		sb.append("<p>");
		sb.append("我们在阐述自己的观点, 意图, 事实时, 有时需要举一些例子或追加一些细节内容来印证自己的观点和意图. 例如:");
		sb.append("</p>");
		sb.append("<p>");
		sb.append("Many factors are important, for example class, gender, age and ethnicity.");
		sb.append("</p>");
		sb.append("<p>");
		sb.append("这些追加的部分通常以下面的单词或短语开头:");
		sb.append("</p>");
		sb.append("<p>");
		sb.append("especially, except, excluding, for example, for instance, including, like, such as. ");
		sb.append("</p>");
		sb.append("<p>");
		sb.append("如果追加的部分没有包含在一个独立的句子中, 就会形成一个残缺句. 例如:");
		sb.append("</p>");
		sb.append("<p>");
		sb.append("For example, leaky pens, candy wrappers, dollar bills, and paperclips.");
		sb.append("</p>");
		return sb.toString();
	}

}
